import dto.AppConfig
import io.github.crackthecodeabhi.kreds.connection.KredsClient
import kotlinx.coroutines.runBlocking
import org.slf4j.Logger

class RedisImpl(
    private val redis: KredsClient,
    private val logger: Logger,
    private val appConfig: AppConfig,
): DatabaseRedis {

    override suspend fun getStartMessage(): String? {
        val x = redis.get("bot start message")
        logger.info("Get start: $x")
        return x
    }

    override suspend fun setStartMessage(text: String) {
        logger.info("Set new start message: $text")
        redis.set("bot start message", text)
    }

    override suspend fun getAdmins(): List<String> {
        val admins = redis.lrange("admins", 0, -1)
        logger.info("Get admins: $admins")
        return admins
    }

    override suspend fun addAdmin(id: String) {
        logger.info("Add admin: $id")
        redis.lpush("admins", id)
    }

    override suspend fun deleteAdmin(id: String) {
        logger.info("Delete admin: $id")
        redis.lrem("admins", 1, id)
    }


    init {
        runBlocking {
            addAdmin(appConfig.admin)
        }
    }
}
